<?php
namespace slinks\extensions\smarty;

use slinks\common\UniversalClassLoader;

use slinks\core\DependencyInjection\Loader\XmlFileLoader;

use slinks\core\dependencyInjectionExt\extension\Extension;

use slinks\extensions\twig\compiler\TwigEnvironmentPass;

use slinks\core\DependencyInjection\ContainerBuilder;
use slinks\core\Config\FileLocator;

/**
 * 
 * 
 * @author Navid Mitchell
 *
 */
class SmartyExtension extends Extension{
	
	const SMARTY_ENGINE = 'smarty.engine';
	const SMARTY_LIB_DIR = 'smarty.lib.dir';
	const SMARTY_TEMPLATE_DIRS = 'smarty.template.dirs';
	const SMARTY_CONFIG_DIRS = 'smarty.config.dirs';
	
    public function configLoad(array $configs, ContainerBuilder $container){
	
    	$this->doSmartyInit($container);

    	if(isset($configs['templateDir'])){
    		$container->setParameter(self::SMARTY_TEMPLATE_DIRS, $configs['templateDir']);
    	}
    	
    	if(isset($configs['configDir'])){
    		$container->setParameter(self::SMARTY_CONFIG_DIRS, $configs['configDir']);
    	}
    	
    	$container->setParameter(self::SMARTY_LIB_DIR,  $configs['smartyLibDir']);
        
    }

    public function pluginDirLoad(array $configs, ContainerBuilder $container){
    	$this->doSmartyInit($container);
    	
    	$container->getDefinition(self::SMARTY_ENGINE)->addMethodCall('addPluginDir', $configs['path']);
    }
    
    private function doSmartyInit(ContainerBuilder $container){
        // we only want to do this once
        if(!$container->has(self::SMARTY_ENGINE)){
        	
	       	$this->addClassesToCompile(array(
	            'Smarty',
	            'Smarty_Internal_Data'
	        ));
	        
	        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/resources/config'));
            $loader->load('smarty.xml');
        }
    }
    
    /**
     * Returns the base path for the XSD files.
     *
     * @return string The XSD base path
     */
    public function getXsdValidationBasePath(){
        return __DIR__.'/resources/schema';
    }

    public function getNamespace(){
        return 'http://www.mitchellsoftware.net/schema/dic/smarty';
    }

    public function getAlias(){
        return 'smarty';
    }
}
